package com.beehoo.dcportal.admin.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.beehoo.dcportal.admin.dao.ComplaintDao;
import com.beehoo.dcportal.common.base.BaseDao;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.ComplaintBean;

public class ComplaintDaoImpl extends BaseDao implements ComplaintDao {

	private static final Logger logger = Logger.getLogger(ComplaintDaoImpl.class);
			
	@Override
	public void auditComplaint(ComplaintBean complaintBean) throws BaseException {
		try {
			sqlMapClient.startTransaction();
			sqlMapClient.getCurrentConnection().setAutoCommit(false);
			int ret = sqlMapClient.update("admin-complaint.auditComplaint", complaintBean);
			logger.info("投诉上报管理-审核投诉上报|#[投诉上报状态：" 
					+ complaintBean.getQueState()
					+ "],[审核人编号：" + complaintBean.getAuditorId()
					+ "],[审核人名称：" + complaintBean.getAuditor()
					+ "],[投诉上报编号：" + complaintBean.getQueId()
					+ "],成功!");
			if (ret >= 0) {
				sqlMapClient.insert("admin-complaint.submitComplaint", complaintBean);
				logger.info("投诉上报管理-创建事务|#[事务编号：" + complaintBean.getAffairId()
						+ "],[事务类型编号：" + complaintBean.getQueType()
						+ "],[服务条线编号：" + complaintBean.getServLine()
						+ "],[服务条线子类编号：" + complaintBean.getServSubline()
						+ "],[固定电话：" + complaintBean.getAuthorTel()
						+ "],[手机号码：" + complaintBean.getAuthorPhone()
						+ "],[反映人姓名：" + complaintBean.getQueAuthor()
						+ "],[家庭地址：" + complaintBean.getAuthorAddr()
						+ "],[事务发生地：" + complaintBean.getQueAddr()
						+ "],[详情记录：" + complaintBean.getQueContent()
						+ "],[登记人编号：" + complaintBean.getAuditorId()
						+ "],成功!");
			}
			
		} catch (SQLException e) {
			try {
				sqlMapClient.getCurrentConnection().rollback();
			} catch (SQLException e1) {
				throw new BaseException("custom.messages.error.admin.operate.error");
			}
			logger.error("投诉上报管理-审核投诉上报|#[投诉上报状态：" 
					+ complaintBean.getQueState()
					+ "],[审核人编号：" + complaintBean.getAuditorId()
					+ "],[审核人名称：" + complaintBean.getAuditor()
					+ "],[投诉上报编号：" + complaintBean.getQueId()
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.complaint.audit.error");
		} finally {
			try {
				sqlMapClient.getCurrentConnection().setAutoCommit(true);
				sqlMapClient.endTransaction();
			} catch (SQLException e) {
				throw new BaseException("custom.messages.error.admin.operate.error");
			}
		}
	}

	@Override
	public int delComplaint(String queId) throws BaseException {
		int ret = 0;
		try {
			ret = sqlMapClient.delete("admin-complaint.delComplaint", queId);
			logger.info("投诉上报管理-删除投诉上报|#[投诉上报编号： " + queId + "成功!");
		} catch (SQLException e) {
			logger.error("投诉上报管理-删除投诉上报|#[投诉上报编号： " + queId + "失败!异常："
					+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.complaint.del.error");
		}
		return ret;
	}

	@Override
	public ComplaintBean queryComplaint(Map<String, Object> paramMap)
			throws BaseException {
		ComplaintBean complaintBean = null;
		try {
			complaintBean = (ComplaintBean) sqlMapClient.queryForObject(
					"admin-complaint.queryComplaintDetail", paramMap);
			logger.info("投诉上报管理-投诉上报详细查询|#[中心编号：" + paramMap.get("streetId")
					+ "],[投诉上报编号：" + paramMap.get("queId") + "],成功!");
		} catch (SQLException e) {
			logger.error("投诉上报管理-投诉上报详细查询|#[中心编号：" + paramMap.get("streetId")
					+ "],[投诉上报编号：" + paramMap.get("queId") + "],失败!异常："
					+ e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.complaint.query.error");
		}
		return complaintBean;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ComplaintBean> queryComplaints(Map<String, Object> paramMap)
			throws BaseException {
		List<ComplaintBean> complaintsLs = null;
		try {
			complaintsLs = sqlMapClient.queryForList(
					"admin-complaint.queryComplaintForList", paramMap);
			logger.info("投诉上报管理-投诉上报列表查询|#[中心编号：" + paramMap.get("streetId")
					+ "],[投诉上报编号：" + paramMap.get("queId")
					+ "],[起始时间：" + paramMap.get("beginDate")
					+ "],[结束时间：" + paramMap.get("endDate")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("投诉上报管理-投诉上报列表查询|#[中心编号：" + paramMap.get("streetId")
					+ "],[投诉上报编号：" + paramMap.get("queId")
					+ "],[起始时间：" + paramMap.get("beginDate")
					+ "],[结束时间：" + paramMap.get("endDate")
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.complaint.query.error");
		}
		return complaintsLs;
	}
	

	@Override
	public int queryComplaintCount(Map<String, Object> paramMap)
			throws BaseException {
		int ret = 0;
		try {
			ret = (Integer) sqlMapClient.queryForObject(
					"admin-complaint.queryComplaintCount", paramMap);
			logger.info("投诉上报管理-投诉上报列表总数查询|#[中心编号：" + paramMap.get("streetId")
					+ "],[投诉上报编号：" + paramMap.get("queId")
					+ "],[起始时间：" + paramMap.get("beginDate")
					+ "],[结束时间：" + paramMap.get("endDate")
					+ "],成功!");
		} catch (SQLException e) {
			logger.error("投诉上报管理-投诉上报列表总数查询|#[中心编号：" + paramMap.get("streetId")
					+ "],[投诉上报编号：" + paramMap.get("queId")
					+ "],[起始时间：" + paramMap.get("beginDate")
					+ "],[结束时间：" + paramMap.get("endDate")
					+ "],失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.complaint.query.error");
		}
		return ret;
	}

	@Override
	public String queryAffairId() throws BaseException {
		String id = null;
		try {
			id = (String) sqlMapClient.queryForObject("admin-complaint.queryAffairId");
			logger.info("投诉上报管理-获取事务编号|成功!");
		} catch (SQLException e) {
			logger.error("投诉上报管理-获取事务编号|失败!异常：" + e.getMessage());
			throw new BaseException(e,
					"custom.messages.error.complaint.submit.error");
		}
		return id;
	}

}
